package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.user.*;
import com.dongdongshop.service.user.AddressService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 用户后台(地址、订单、物流等等)
 * @Author Eayon
 * @Date 2020/3/14 10:19
 */
@Controller
@RequestMapping("userhome")
public class UserHomeController {

    private static final Logger log = LoggerFactory.getLogger(UserHomeController.class);

    @Reference(check = false)
    private AddressService addressService;

    /**
     * 跳转用户后台
     * @return
     */
    @RequestMapping("toUserHome")
    public String toUserHome(){
        return "userhome/home-index";
    }

    /**
     * 调换用户地址管理
     * @return
     */
    @RequestMapping("toAddress")
    public String toAddress(Model model){
        //获取当前登录用户
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        //根据当前登录的用户id查询相关联的地址信息
        List<TbAddress> addresses = addressService.getAddressByUserId(user.getId());
        model.addAttribute("addresses",addresses);
        return "userhome/home-setting-address";
    }

    /**
     * 获取所有省信息
     */
    @RequestMapping("findProvincesAll")
    @ResponseBody
    public DataResult findProvincesAll(){
        List<TbProvinces> provinces = null;
        try {
            provinces = addressService.findProvincesAll();
        }catch (Exception e){
            log.error("获取所有省信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),provinces);
    }

    /**
     * 根据省份id获取市级信息
     * @param provinceid
     * @return
     */
    @RequestMapping("getCitiesByProvinceid")
    @ResponseBody
    public DataResult getCitiesByProvinceid(String provinceid){
        List<TbCities> cities = null;
        try {
            cities = addressService.getCitiesByProvinceid(provinceid);
        }catch (Exception e){
            log.error("根据省份id获取市级信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),cities);
    }

    /**
     * 根据市级id查询县区信息
     * @param cityid
     * @return
     */
    @RequestMapping("getAreasByCityid")
    @ResponseBody
    public DataResult getAreasByCityid(String cityid){
        List<TbAreas> areas = null;
        try {
            areas = addressService.getAreasByCityid(cityid);
        }catch (Exception e){
            log.error("根据市级id查询县区信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),areas);
    }

    /**
     * 增加用户收获地址
     * @param address
     */
    @RequestMapping("addAddress")
    @ResponseBody
    public DataResult addAddress(TbAddress address){
        try {
            //获取当前用户
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getId().toString());
            address.setCreateDate(new Date());
            //增加
            addressService.addAddress(address);
        }catch (Exception e){
            log.error("增加用户收货地址异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }

}
